package com.jams.goodox.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.flyco.tablayout.SlidingTabLayout;
import com.jams.goodox.R;
import com.jams.goodox.adapter.SmartPagerAdapter;

import java.util.List;

/**
 *
 * Created by DWCloud on 2016/4/19.
 */
public class ViewUtils {
    private static long lastClickTime;

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if ((0 < timeD && timeD < 500) || timeD < 0) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    public static void setMargins(View v, int l, int t, int r, int b) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            p.setMargins(l, t, r, b);
            v.requestLayout();
        }
    }

    public static void setRelativeLayoutWH(View view, int width, int height) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }

    public static void setLinearLayoutWH(View view, int width, int height) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }

    public static void setRecyclerViewWH(View view, int width, int height) {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }

    public static void setAbsListViewLayoutWH(View view, int width, int height) {
        AbsListView.LayoutParams params = (AbsListView.LayoutParams) view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }

    public static void setViewGroupWH(View view, int width, int height) {
        ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) view.getLayoutParams(); //取控件当前的布局参数
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }

    public static void tabsBindPager(SlidingTabLayout mTabLayout, List<String> tabsTitle, ViewPager pager, List<Fragment> fragmentList, FragmentManager fm) {
        try {
            Context context = mTabLayout.getContext();
            DisplayMetrics dm = new DisplayMetrics();
            ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);

            if (tabsTitle.size() < 5 && tabsTitle.size() > 0) {
                float wid = DensityUtil.px2dp(context, (float) dm.widthPixels) / tabsTitle.size();
                mTabLayout.setTabWidth(wid);
            } else {
                mTabLayout.setTabWidth(DensityUtil.px2dp(context, (float) dm.widthPixels) / 4);
            }
            SmartPagerAdapter adapter = new SmartPagerAdapter(fm, fragmentList, tabsTitle);
            pager.setAdapter(adapter);
            mTabLayout.setViewPager(pager);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void addView(Context context, ViewGroup view, LinearLayout layout, int icon, int text) {
        removeView(view, layout);
        if (layout == null) {
            return;
        }
        layout.findViewById(R.id.no_icon).setBackgroundResource(icon);
        TextView tv = (TextView) layout.findViewById(R.id.no_text);
        tv.setText(text);
        view.addView(layout);
    }

    public static void removeView(ViewGroup view, LinearLayout layout) {
        if (view != null) {
            view.removeView(layout);
        }
    }

    //获取小图链接
    public static String getSmallUrl(String url) {
        if (url == null || url.equals("")) {
            return "";
        }
        int Suffix = url.length() - url.lastIndexOf(".");
        String newUrl = url.substring(0, url.length() - Suffix) + "_small" + url.substring(url.length() - Suffix, url.length());
        return newUrl;
    }

    public static void setText(TextView t, String text) {
        if (TextUtils.isEmpty(text)) {
            t.setText("");
        } else {
            text = text.trim();
            t.setText(text);
        }
    }

    public static void setText(TextView t, String insert, String text) {
        if (TextUtils.isEmpty(text)) {
            t.setText("");
        } else {
            text = text.trim();
            t.setText(insert + text);
        }
    }

    public static String getText(TextView t) {
        return t.getText().toString().trim();
    }

    public static String getText(EditText t) {
        return t.getText().toString().trim();
    }

    public static MaterialDialog getExitEditDialog(final Context context, String msg) {
        MaterialDialog dialog = new MaterialDialog.Builder(context).content(msg).negativeText("取消").onNegative(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                dialog.cancel();
            }
        }).positiveText("确定").onPositive(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                dialog.cancel();
                ((Activity) context).finish();
            }
        }).build();
        return dialog;
    }

    public static MaterialDialog getExitEditDialog(final Context context, String msg, MaterialDialog.SingleButtonCallback callback) {
        MaterialDialog dialog = new MaterialDialog.Builder(context).content(msg).negativeText("取消").onNegative(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                dialog.cancel();
            }
        }).positiveText("确定").onPositive(callback).build();
        return dialog;
    }

    /**
     * 判断app是否在后台运行
     *
     * @param context
     * @return
     */

    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                /*
                BACKGROUND=400 EMPTY=500 FOREGROUND=100
				GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
				 */
                /*Log.i(context.getPackageName(), "此appimportace ="
						+ appProcess.importance
						+ ",context.getClass().getName()="
						+ context.getClass().getName());*/

				/*Log.i(context.getPackageName(), "处于后台"
						+ appProcess.processName);*/

                /*	Log.i(context.getPackageName(), "处于前台"
                            + appProcess.processName);*/
                return appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
            }
        }
        return false;
    }

    /**
     * 按比例获得图片的高度
     *
     * @param cHeight 压缩比例高
     * @param cWidth  压缩比例宽
     * @param rWidth  压缩后宽
     * @return 压缩后高
     */
    public static float getCompressHeight(float cHeight, float cWidth, float rWidth) {
        float height;
        height = (cHeight * rWidth) / cWidth;
        return height;
    }

    /**
     * 修改标签宽度 ,企业合作用到
     * @param mContext mContext
     * @param textView textView
     */
    public static void setLabelWidth(Context mContext,final TextView textView) {
        if (textView != null && textView.getText().toString().trim().length() > 0) {
            ViewGroup.LayoutParams params = textView.getLayoutParams();
            final int labelWords = textView.getText().toString().trim().length();
            float labelWidth = 1f;
            switch (labelWords) {
                case 2:
                    labelWidth = 44f;
                    break;
                case 3:
                    labelWidth = 58f;
                    break;
                case 4:
                    labelWidth = 71f;
                    break;
            }
            params.width = (DensityUtil.dp2px(mContext, labelWidth));
            textView.setLayoutParams(params);
        }
    }
}
